<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>邮编与结构化数据 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_postcodes_and_structured_data.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_postcodes_and_structured_data.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_postcodes_and_structured_data.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_postcodes_and_structured_data.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="search-in-depth.html">深入搜索</a></span>
»
<span class="breadcrumb-link"><a href="partial-matching.html">部分匹配</a></span>
»
<span class="breadcrumb-node">邮编与结构化数据</span>
</div>
<div class="navheader">
<span class="prev">
<a href="partial-matching.html">« 部分匹配</a>
</span>
<span class="next">
<a href="prefix-query.html">prefix 前缀查询 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_postcodes_and_structured_data"></a>邮编与结构化数据<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/130_Partial_Matching/05_Postcodes.asciidoc">edit</a>
</h2>
</div></div></div>
<p>我们会使用美国目前使用的邮编形式（United Kingdom postcodes 标准）来说明如何用部分匹配查询结构化数据。这种邮编形式有很好的结构定义。例如，邮编 <code class="literal">W1V 3DG</code> 可以分解成如下形式：</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<p><code class="literal">W1V</code> ：这是邮编的外部，它定义了邮件的区域和行政区：</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<code class="literal">W</code> 代表区域（ 1 或 2 个字母）
</li>
<li class="listitem">
<code class="literal">1V</code> 代表行政区（ 1 或 2 个数字，可能跟着一个字符）
</li>
</ul>
</div>
</li>
<li class="listitem">
<p><code class="literal">3DG</code> ：内部定义了街道或建筑：</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<code class="literal">3</code> 代表街区区块（ 1 个数字）
</li>
<li class="listitem">
<code class="literal">DG</code> 代表单元（ 2 个字母）
</li>
</ul>
</div>
</li>
</ul>
</div>
<p>假设将邮编作为 <code class="literal">not_analyzed</code> 的精确值字段索引，所以可以为其创建索引，如下：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">PUT /my_index
{
    "mappings": {
        "address": {
            "properties": {
                "postcode": {
                    "type":  "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/130_Partial_Matching/10_Prefix_query.json"></div>
<p>然后索引一些邮编：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">PUT /my_index/address/1
{ "postcode": "W1V 3DG" }

PUT /my_index/address/2
{ "postcode": "W2F 8HW" }

PUT /my_index/address/3
{ "postcode": "W1F 7HW" }

PUT /my_index/address/4
{ "postcode": "WC1N 1LZ" }

PUT /my_index/address/5
{ "postcode": "SW5 0BE" }</pre>
</div>
<div class="sense_widget" data-snippet="snippets/130_Partial_Matching/10_Prefix_query.json"></div>
<p>现在这些数据已可查询。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="partial-matching.html">« 部分匹配</a>
</span>
<span class="next">
<a href="prefix-query.html">prefix 前缀查询 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>